//
//  ShareDetailsFriendView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/6.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class ShareDetailsFriendView: UIView {
    var topLabel:UILabel = UILabel()
    var timeLabel:UILabel = UILabel()
    var reLabel:UILabel = UILabel()
    var inviteBlock:(() -> Void)?
    
    var model = [UserInfoModel]()
    var userModel: [UserInfoModel]? {
        didSet {
            guard let model = userModel else { return }
            self.model = model
            setCentre()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        self.topLabel = UILabel.init(frame: FRAME_CGFloat(0, 10, SCREEN_WIDTH, 20))
        self.topLabel.font = Font_Medium(14)
        self.topLabel.textColor = RGB_Same(53)
        self.topLabel.text = "还差3个助力就可以免费领课程，快去求助哦！"
        self.topLabel.textAlignment = .center
        self.addSubview(self.topLabel)

        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(37.5, 118, SCREEN_WIDTH - 75, 44)
        btn.setBackgroundImage(IMAGE(name: "Rectangle"), for: .normal)
        btn.setTitle("邀请好友助力", for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        btn.addTarget(self, action: #selector(inviteBtnAction), for: .touchUpInside)
        self.addSubview(btn)
        
        self.reLabel = UILabel.init(frame: FRAME_CGFloat(0, 193.5, SCREEN_WIDTH, 24))
        self.reLabel.font = Font_Medium(17)
        self.reLabel.textColor = RGB_Same(53)
        self.reLabel.textAlignment = .center
        self.reLabel.text = "还剩                     结束"
        self.addSubview(self.reLabel)
        
        self.timeLabel = UILabel.init(frame: FRAME_CGFloat(0, 191.5, SCREEN_WIDTH, 28))
        self.timeLabel.font = Font_Semibold(20)
        self.timeLabel.textColor = RGB(230, 67, 64)
        self.timeLabel.textAlignment = .center
        self.addSubview(self.timeLabel)
    }
    
    func setCentre() {
        let num = 3 - self.model.count
        self.topLabel.text = "还差\(num)个助力就可以免费领课程，快去求助哦！"
        let frien = CentrelFriendView.init(frame: FRAME_CGFloat(9, 34, SCREEN_WIDTH - 18, 74))
        frien.userModel = self.model
        self.addSubview(frien)
    }
    
    @objc func inviteBtnAction() {
        inviteBlock?()
    }
    
    func setDate(shareTime:Int, survivalTime:Int) {
        let reTime = shareTime + survivalTime
        let nowTime = Int(Date().timeIntervalSince1970)
        
        if reTime > nowTime {
            let tt = reTime - nowTime
            self.timingAction(tt: tt)
            
        } else {
            self.reLabel.text = ""
            self.timeLabel.text = "已结束"
        }
        
    }
    
    func timingAction(tt:Int) {
        
        var time = tt
        let codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
        codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000))
        codeTimer.setEventHandler {
            
            time = time - 1
            
            if time < 0 {
                codeTimer.cancel()
                DispatchQueue.main.async {
                    self.reLabel.text = ""
                    self.timeLabel.text = "已结束"
                }
                return
            }
            
            DispatchQueue.main.async {
                self.timeLabel.text = String.returnTimeTofammater(times: TimeInterval(time))
            }
            
        }
        
        if #available(iOS 10.0, *) {
            codeTimer.activate()
        } else {
            self.reLabel.text = ""
            self.timeLabel.text = "已结束"
        }
        
        
    }

}
